package cn.anecansaitin.whimsy.common.network;

import cn.anecansaitin.whimsy.Whimsy;
import cn.anecansaitin.whimsy.client.registers.ModSounds;
import cn.anecansaitin.whimsy.common.register.ModEffects;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.player.Player;
import net.neoforged.neoforge.network.handling.PlayPayloadContext;

import java.util.Optional;

public record ServerRocketPunchStopPack() implements CustomPacketPayload {
    public static final ResourceLocation ID = Whimsy.resource("server_rocket_punch_stop_pack");
    public static final ServerRocketPunchStopPack.ServerHandler SERVER_HANDLER = new ServerRocketPunchStopPack.ServerHandler();

    public ServerRocketPunchStopPack(FriendlyByteBuf buffer) {
        this();
    }

    @Override
    public void write(FriendlyByteBuf buffer) {

    }

    @Override
    public ResourceLocation id() {
        return ID;
    }

    public static class ServerHandler {
        private ServerHandler() {}

        public void handle(ServerRocketPunchStopPack data, PlayPayloadContext context) {
            context.workHandler().submitAsync(() -> {
                Optional<Player> optionalPlayer = context.player();

                if (optionalPlayer.isEmpty()) {
                    return;
                }

                Player player = optionalPlayer.get();
                player.removeEffect(ModEffects.ROCKET_PUNCH.get());
                player.level().playSound(null, player.getX(), player.getY(), player.getZ(), ModSounds.ROCKET_PUNCH_HIT.get(), SoundSource.PLAYERS);
            });
        }
    }
}
